<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @close="closeBtn">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="rolesId">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      <!-- 选项 -->
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="closeBtn">取消</el-button>
      </el-col>
    </el-row>
    </el-checkbox-group></el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      list: [], // 所有角色列表
      rolesId: [] // 当前拥有的角色id
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async btnOk() {
      await assignRoles({ id: this.userId, roleIds: this.rolesId })
      this.$emit('update:showRoleDialog', false)
    },
    closeBtn() {
      this.rolesId = []
      this.$emit('update:showRoleDialog', false)
    },
    async getRoleList() {
      const { rows } = await getRoleList()
      this.list = rows
    },
    async getUserDetailById(id) {
      // props传值是异步的
      const { roleIds } = await getUserDetailById(id)
      this.rolesId = roleIds || []
    }
  }
}
</script>
